﻿$(document).ready(function () {
    var url = new Url();
    var $css = url.GetQueryString("css");
    //分页设置
    var page = new Page();
    page.Dom = $("#searchArea");
    page.CallBack = function (data) {
        $(".contractInfo").empty();
        var isCredit = $("#displayMode").attr("data-credit") === "True"
        var isSettlement = $("#displayMode").attr("data-settlement") === "True";

        var loan = $("#contractType").val() !== "理财流程";
        $.each(data, function (index, item) {
            item.Css = $css;
            item.Verifiable = item.Verifiable ? "inline" : "none";
            item.IsCredit = isCredit;
            item.IsSettlement = isSettlement;
            item.ViewPage = loan ? (isCredit ? "LoanCreditPreview" : "LoanOrderPreview") : "FinancialOrderDetail";
            item.IsLoan = loan;
            $("#ContractTemplete").tmpl(item).appendTo($(".contractInfo"));
        });
    };

    //权限判断
    if ($("#contractType option").length > 0) {

        var contractType = new ContractType();

        //业务类型下拉列表触发
        $("#contractType").change(function () {
            var typeName = $("#contractType").val();
            if (typeName == "理财流程") {
                page.Url = "../Handler/FinancialOrder.ashx";
                page.type = 8;
            }
            else {
                page.Url = "../Handler/LoanProductOrderHandler.ashx";
                page.type = 18;
            }
            var typeName = $("#contractType").val();
            var statusList = contractType.GetContractType(typeName);
            $("#verifyStatus").html(statusList);
        }).change();

        $("#btnSearch").click(function () { page.GetPageData(); }).click();

    } else {
        $("#caption").text("暂无数据！");
    }

    //搜索用户
    $(".btnSearchBorrow").bind("click", function () {
        var $userName = $(".txtName").val();
        var $phone = $(".txtPhone").val();
        $.getJSON("../Handler/FinancialUser.ashx",
            { UserName: $userName, Phone: $phone, type: 10, _: Math.random() },
            function (result) {
                if (result.Status) {
                    //清空用户列表
                    $(".userInfo").empty();
                    //绑定模板
                    $(result.Data).each(function (index, item) {
                        $("#UserTemplete").tmpl(item).appendTo($(".userInfo"));
                    });
                    //绑定单击行事件
                    $(".userInfo tr").css("cursor", "pointer").click(function () {
                        $(".txtuserName").val($(this).find(".name").text());
                        $(".txtTel").val($(this).find(".phone").text());
                        $(".txtUserId").val($(this).find(".userId").text());
                    });
                }
            });
    });

    $(".btnDelLender").bind("click", function () {
        $(".txtuserName").val("");
        $(".txtUserId").val("");
    });
    //选择用户
    $(".btnShow").bind("click", function () {
        $(".txtName").val($(".txtUserName").val());
    })
    //单击用户名文本框时选择用户
    $(".txtuserName").bind("click", function () {
        $(".btnShow").click();
    });

});

var ContractType = function () { };
ContractType.prototype = {
    GetContractType: function (typeName) {
        var result = "";
        $.ajax({
            type: "post",
            url: "../Handler/VerifyStatusHandler.ashx",
            dataType: "json",
            data: { type: 1, FolwName: typeName },
            async: false,
            cache: true,
            complete: function () {
            },
            success: function (data) {
                if (data.Status) {
                    var str = '<option value="">请选择</option>';
                    $(data.Data).each(function (i, e) {
                        str += '<option value="' + e.VerifyId + '">' + e.VerifyName + '</option>';
                    });
                    result = str;
                }
                else {
                    alert(data.Message);
                }
            }
        });
        return result;
    }
};